package com.speedcar.base.autoconfigure;


import com.speedcar.base.advice.ScExceptionAdvice;
import com.speedcar.base.advice.ScResponseAdvice;
import com.speedcar.base.aspect.ScRateLimitAspect;
import com.speedcar.base.aspect.ScLogAspect;
import com.speedcar.base.result.ScDefaultResultWrapper;
import com.speedcar.base.result.ScResultWrapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableConfigurationProperties(ScBaseProperties.class)
public class ScBaseAutoConfiguration {

    private final ScBaseProperties scBaseProperties;

    public ScBaseAutoConfiguration(ScBaseProperties scBaseProperties) {
        this.scBaseProperties = scBaseProperties;
    }

    @Bean
    @ConditionalOnMissingBean(ScResultWrapper.class)
    public ScResultWrapper defaultResultWrapper() {
        return new ScDefaultResultWrapper();
    }

    @Bean
    @ConditionalOnMissingBean(ScResponseAdvice.class)
    @ConditionalOnProperty(name = "speedcar.base.enable-response", havingValue = "true", matchIfMissing = true)
    public ScResponseAdvice scResponseAdvice() {
        return new ScResponseAdvice();
    }

    @Bean
    @ConditionalOnMissingBean(ScExceptionAdvice.class)
    public ScExceptionAdvice scExceptionAdvice() {
        return new ScExceptionAdvice();
    }

    @Bean

    @ConditionalOnMissingBean(ScLogAspect.class)
    @ConditionalOnProperty(name = "speedcar.base.enable-log", havingValue = "true", matchIfMissing = true)
    public ScLogAspect scLogAspect() {
        return new ScLogAspect();
    }

    @Bean
    @ConditionalOnMissingBean(ScRateLimitAspect.class)
    public ScRateLimitAspect scRateLimitAspect() {
        return new ScRateLimitAspect();
    }

}
